package com.lvyq.myplayer.ui.activity.ranking

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.current.CurrentPlayLstAdapter
import com.lvyq.myplayer.base.BaseActivity
import com.lvyq.myplayer.common.shared.MainAudioSharedPrefsManager
import com.lvyq.myplayer.common.util.MockDataUtil
import com.lvyq.myplayer.service.AudioService
import com.lvyq.myplayer.ui.fragment.ranking.TheChartFragment

/**
 * 文件描述：TheChartActivity 排行榜
 * 作者：lvyq
 * 创建时间：2025-05-29
 * 修改时间：2025-05-29
 */
class TheChartActivity : BaseActivity() {

    var rankingType= MockDataUtil.rankingTypeList()
    override var currentPlayAdapter: CurrentPlayLstAdapter? = null

    lateinit var rankingTypeTab:TabLayout
    lateinit var rankTypeView:ViewPager2
    override fun getLayoutId(): Int {
       return R.layout.activity_index_ranking
    }

    override fun initData() {
        super.initData()
        initPlayBoxService()
        rankingTypeTab =findViewById(R.id.rankingTypeTab)
        rankTypeView = findViewById(R.id.rankTypeView)

        //viewpage初始化
        rankTypeView.adapter=object :FragmentStateAdapter(this){
            override fun getItemCount(): Int {
               return rankingType.size
            }

            override fun createFragment(position: Int): Fragment {
                var fragment= TheChartFragment()
                val args = Bundle().apply {
                    putInt("RANKING_TYPE", position)
                }
                fragment.arguments=args
                return fragment
            }

        }
        // 关联 TabLayout 和 ViewPager2
        TabLayoutMediator(rankingTypeTab, rankTypeView) { tab, position ->
            tab.text = rankingType[position].name.toString()
        }.attach()
        // 默认选中第一个页面
        rankTypeView.setCurrentItem(0, true)
    }



    /**
     * 播放栏服务初始化
     */
    private fun initPlayBoxService() {
        initMainPlayBoxListener()
        currentPlayAdapter=getAdapter()
        initAudioPlayLst(this)

        //设置viewpage2 适配器
        recreateViewPagerAdapter()
        initMainPlayStateChangeUI()
        // updateMainPlayStateBtn(false)
        var songInfo = MainAudioSharedPrefsManager.getSongInfo(this)
        var currentAudioPosition = MainAudioSharedPrefsManager.getCurrentAudioPosition(this)
        //通过audioservice 播放,混合启动，保证退出当前页仍然播放
        val audioIntent = Intent(this, AudioService::class.java)
        //传递值到AudioService中
        audioIntent.putExtra("data",songInfo)
        audioIntent.putExtra("from",-1)
        audioIntent.putExtra("position",currentAudioPosition?.toInt())
        //绑定 BIND_AUTO_CREATE 服务未创建时创建并绑定
        bindService(audioIntent,connection, BIND_AUTO_CREATE)
        //启动
        startService(audioIntent)
    }

    private fun getAdapter(): CurrentPlayLstAdapter {
        return CurrentPlayLstAdapter(this)
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}